package com.cloudcc.mobile.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import butterknife.Bind;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.view.base.BaseActivity;
/**
 * 
 * 根据坐标显示地图信息
 * @author lhy
 * 2015/12/03
 */
public class ShowBaiduMapActivity extends BaseActivity implements OnClickListener{
	@Bind(R.id.bmapview)
	MapView mMapView;
	@Bind(R.id.bar_back)
	ImageView back;
	@Bind(R.id.bar_title_text)
	TextView title;
	private InfoWindow mInfoWindow;
	private BaiduMap mBaiduMap;
//	private String city="济宁";
	private String address = "历城区经十路";
	private BitmapDescriptor bitmap = null;
	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		title.setText("位置信息");
		back.setOnClickListener(this);
		initData();
		initbaidumap();
		getMapAddress(null);
	}
	private void initData() {
		Intent intent = getIntent();
		address = intent.getStringExtra("address");
//		city = intent.getStringExtra("city");
//		address = intent.getStringExtra("address");
	}
	//初始化地图
	private void initbaidumap() {
		mBaiduMap = mMapView.getMap();
		mMapView.showZoomControls(false);
		MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
		mBaiduMap.setMapStatus(msu);
		mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
			
			@Override
			public boolean onMapPoiClick(MapPoi arg0) {
				// TODO Auto-generated method stub
				showMarkOverlay(arg0.getPosition(), arg0.getName());
				return true;
			}
			
			@Override
			public void onMapClick(LatLng arg0) {
				getMapAddress(arg0);
				
			}
		});
	}
	//展示坐标地图
	private void showbaidumap(LatLng point1) {
		if(bitmap == null){
			bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ease_icon_marka);
		}
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(point1);
		OverlayOptions option = new MarkerOptions().position(point1).icon(bitmap);
		mBaiduMap.addOverlay(option);
		mBaiduMap.animateMapStatus(u);
	}
	//设置反地理编码位置坐标 
	public void getMapAddress(final LatLng point){
		GeoCoder geoCoder = GeoCoder.newInstance(); 
		if(point == null){
			geoCoder.geocode(new GeoCodeOption().city("")  
			    .address(address));
		}else{
			geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(point));
		}
		
        geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { 
               //反地理编码/坐标转地址
            @Override 
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult coderesult) { 
                //获取点击的坐标地址 
                showMarkOverlay(point,coderesult.getAddress());
            } 
               //反地理编码/地址转坐标
            @Override 
            public void onGetGeoCodeResult(GeoCodeResult result) { 
            	if(result.getLocation() != null)
                showbaidumap(result.getLocation());
            } 
        });
        
	}
	//展示位置信息气泡
	private void showMarkOverlay(LatLng latLng,String address) {
//		getMapAddress(latLng);
		TextView viewContent = (TextView) View.inflate(mContext,
				R.layout.map_make_item, null);
		viewContent.setText(address);
		mInfoWindow = new InfoWindow(viewContent, latLng, 0);
		mBaiduMap.showInfoWindow(mInfoWindow);
	}
	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.showbaidumap;
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mMapView.onResume();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mMapView.onDestroy();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mMapView.onPause();
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.bar_back:
			this.finish();
			break;

		default:
			break;
		}
	}
	
}
